//
//  YTMCollectionController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/24.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTMCollectionController.h"
#import "YTHomeArticleCell.h"
#import "YTNoDataCell.h"


NSString *colloectNoDataCellID = @"noDataCellID";

NSString *McollectionCellID = @"McollectionCellID";
@interface YTMCollectionController ()

@property(nonatomic ,strong)YTHomeMainModel *model;

@property(nonatomic ,assign)int page;

@property(nonatomic ,assign)BOOL isNoData;

@end

@implementation YTMCollectionController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.page = 1;
    
   
    
    [self setUI];
    [self.navigationController.navigationBar setBarTintColor:UIColor.whiteColor];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_header.state = MJRefreshStatePulling;

}

- (void)setUI
{
    self.navigationItem.title = self.isHistory ? @"浏览历史" : @"我的收藏";
    [self.navigationController.navigationBar setBarTintColor:UIColor.lightGrayColor];
    
    YTRefreshHeader *header = [YTRefreshHeader headerWithRefreshingBlock:^{
        [self loadDataWithPullUp:NO];
    }];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadDataWithPullUp:YES];
    }];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YTHomeArticleCell" bundle:nil]
         forCellReuseIdentifier:McollectionCellID];
    
    self.tableView.backgroundColor = [UIColor colorWithHex:@"f1f2f2"];
    self.tableView.mj_header = header;
    self.tableView.mj_footer = footer;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [footer setTitle:@"没有更多了哦~" forState:MJRefreshStateNoMoreData];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    
    self.tableView.rowHeight = 88;
   
    
    [self.navigationController.navigationBar setTintColor:kBackButtonColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"倛袨1"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClick)];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"YTNoDataCell" bundle:nil] forCellReuseIdentifier:colloectNoDataCellID];

}

// 返回按钮
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:true];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.subviews[0].subviews[0].hidden = false;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

}
#pragma mark - 加载数据
- (void)loadDataWithPullUp:(BOOL)pullup
{
    if (self.model) {
        if (pullup) {
            self.page++;
            if (self.model.list.count > self.model.total.intValue) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
                return;
            }
        }else{
            self.page = 1;
        }
    }
    
    [YTNetworkManager.shareManager requestCollectionListWithIshistory:self.isHistory
                                                                paged:self.page page_size:10
                                                           completion:^(YTHomeMainModel *model, BOOL isSuccess, NSString *error)
    {
        if (!isSuccess) {
            [self.view showTipWithTipStr:error];
            pullup ? [self.tableView.mj_footer endRefreshing] : [self.tableView.mj_header endRefreshing];
            return ;
        }
        
        if (pullup) {
            self.model.list = [self.model.list arrayByAddingObjectsFromArray:model.list];
            [self.tableView.mj_footer endRefreshing];
        }else{
            self.model = model;
            
            self.isNoData = !(model.list.count > 0);

            [self.tableView.mj_header endRefreshing];
        }
        
        [self.tableView reloadData];
    }];
}



#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.isNoData ? 1 : self.model.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YTHomeArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:self.isNoData ? colloectNoDataCellID : McollectionCellID
                                                               forIndexPath:indexPath];
    
    if (!self.isNoData) {
        [(YTHomeArticleCell*)cell setArticle:self.model.list[indexPath.row]];
        
    }else{
        [(YTNoDataCell*)cell setImage:[UIImage imageNamed:self.isHistory ? @"yuedu_ss_yd" : @"shoucang_ss_sc"]];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.isNoData) {
        return;
    }
    YTHomeDetailController *detailVC = [[YTHomeDetailController alloc] init];
    detailVC.ID = self.model.list[indexPath.row].ID;
    [self.navigationController pushViewController:detailVC animated:false];
}
#pragma mark: - table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.isNoData ? kScreenHeight - kNavbarHeight : 88;
}

@end
